// pages/goods_list/goods_list.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs: [
      {
        id: 0,
        value: "综合",
        isActive: true
      },
      {
        id: 1,
        value: "价格↓",
        isActive: false
      },
      {
        id: 2,
        value: "价格↑",
        isActive: false
      }
    ],
    goodsList: [],
    goodsListj: [],
    goodsLists: [],
    cid: '',
    pagenum: "1",
    pagesize: "10",
    totalPages: ''
  },
  // 接口要的参数
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.cid = options.cid;
    this.getGoodsList();
    this.getGoodsListj();
    this.getGoodsLists();
  },


  // 获取商品数据
  getGoodsList() {
    wx: wx.request({
      url: 'https://api-hmugo-web.itheima.net/api/public/v1/goods/search?pagesize=99&cid=' + this.cid  + '',
      success: (res) => {
        // console.log(res.data.message)
        var newList = res.data.message.goods;
        this.setData({
          goodsList: newList,
        })
      }
    })
  },
  getGoodsListj() {
    wx: wx.request({
      url: 'https://api-hmugo-web.itheima.net/api/public/v1/goods/search?pagesize=99&cid=' + this.cid  + '',
      success: (res) => {
        // console.log(res.data.message)
        var newList = res.data.message.goods;
        var price = 'goods_price';
        this.setData({
          goodsListj: newList.sort(this.comparej(price)),
        })
      }
    })
  },
  getGoodsLists() {
    wx: wx.request({
      url: 'https://api-hmugo-web.itheima.net/api/public/v1/goods/search?pagesize=99&cid=' + this.cid  + '',
      success: (res) => {
        // console.log(res.data.message)
        var newList = res.data.message.goods;
        var price = 'goods_price';
        this.setData({
          goodsLists: newList.sort(this.compares(price)),
        })
      }
    })
  },
  // 标题点击事件 从子组件传递过来
  handleTabsItemChange(e) {
    //获取被点击的标题索引
    const { index } = e.detail;
    // console.log(e.detail)
    //修改源数组
    let { tabs } = this.data;
    tabs.forEach((v, i) => i === index ? v.isActive = true : v.isActive = false);
    // 赋值
    this.setData({
      tabs
    })
  },
  // 排序函数
  comparej: function (property) {
    return function (a, b) {
      var value1 = a[property];
      var value2 = b[property];

        return value2 - value1;
      }
  },
  compares: function (property) {
    return function (a, b) {
      var value1 = a[property];
      var value2 = b[property];

        return value1 - value2;
      }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
   
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})